package com.zx.erp.market.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zx.erp.market.bean.Orderdetails;
import com.zx.erp.market.bean.Orders;
import com.zx.erp.market.mapper.OrderdetailsMapper;
import com.zx.erp.market.mapper.OrdersMapper;
import com.zx.erp.market.service.OrderdetailsService;
import com.zx.erp.market.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

@Service
public class OrdersBizImpl implements OrdersService {
    @Autowired
    private OrdersMapper odao;
    @Autowired
    private OrderdetailsMapper oddao;
    @Autowired
    private OrderdetailsService odbiz;

    @Override
    public PageInfo<Orders> findPageAll(Orders orders, List<Integer> cidList, Date dateMin, Date dateMax,
                                        Double priceMin, Double priceMax, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Orders> pageInfo = new PageInfo<>(odao.selectAll(orders, cidList, dateMin, dateMax, priceMin, priceMax));
        return pageInfo;
    }

    @Override
    public List<Orders> findAll(Orders orders) {
        return odao.selectAll(orders, null, null, null, null, null);
    }

    @Override
    public Orders findBy(String orderid) {
        return odao.selectByPrimaryKey(orderid);
    }

    @Override
    public boolean editOrdersAndOrderDS(Orders orders, List<Orderdetails> oDerdetas) {
        boolean flag = false;
        try {
            flag=oddao.deleteOrderDsOrderId(orders.getOrderid());
            flag = odao.updateByPrimaryKeySelective(orders);
            flag=odbiz.addOrderDs(oDerdetas);
        } catch (Exception e) {
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public boolean addOrders(Orders orders, List<Orderdetails> oDerdetas) {
        boolean flag = false;
        try {
            flag = odao.insertSelective(orders);
            boolean a = flag;
            for (Orderdetails od : oDerdetas) {
                od.setOrderId(orders.getOrderid());
                flag = oddao.insertSelective(od);
                if(!flag){
                    break;
                }
            }
            flag=a;
        } catch (Exception e) {
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public boolean editStatus(String orderid, Integer uid) {
        Orders order = odao.selectByPrimaryKey(orderid);
        order.setStatus(2);
        order.setCheckid(uid);
        order.setChectime(new Date(System.currentTimeMillis()));
        boolean flag = false;
        try {
            flag = odao.updateByPrimaryKeySelective(order);
        } catch (Exception e) {
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public boolean editOrders(Orders orders) {
        return odao.updateByPrimaryKeySelective(orders);
    }

    @Override
    public boolean delOrders(String orderid) {
        boolean flag=false;
        try {
            flag=odao.deleteByPrimaryKey(orderid);
            boolean aa=flag;
            flag=oddao.deleteOrderDsOrderId(orderid);
            flag=aa;
        } catch (Exception e) {
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public List<Orders> findNotCkWOrderid() {
        return odao.selectNotCkWOrderid();
    }

    @Override
    public PageInfo<Orders> findAllFZ(List<Integer> cidList1,List<Integer> cidList2, Date dateMin, Date dateMax, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Orders> pageInfo = new PageInfo<>(odao.selectAllFZ(cidList1,cidList2, dateMin, dateMax));
        return pageInfo;
    }
}
